package com.su.middleware;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;

import com.su.middleware.util.ThesisUtils;

public class ProcessStarterBean {
	private String title;
	private String annotation;
	private String description;
	private String programmingTechnologies;
	private int executionTime;
	private String consultantName;
	private String supervisor;
	private String student;
	
	public ProcessStarterBean() {
		title = "";
		annotation = "";
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}
	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}
	/**
	 * @return the annotation
	 */
	public String getAnnotation() {
		return annotation;
	}
	/**
	 * @param annotation the annotation to set
	 */
	public void setAnnotation(String annotation) {
		this.annotation = annotation;
	}
	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}
	/**
	 * @param description the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}
	/**
	 * @return the programmingTechnologies
	 */
	public String getProgrammingTechnologies() {
		return programmingTechnologies;
	}
	/**
	 * @param programmingTechnologies the programmingTechnologies to set
	 */
	public void setProgrammingTechnologies(String programmingTechnologies) {
		this.programmingTechnologies = programmingTechnologies;
	}
	/**
	 * @return the executionTime
	 */
	public int getExecutionTime() {
		return executionTime;
	}
	/**
	 * @param executionTime the executionTime to set
	 */
	public void setExecutionTime(int executionTime) {
		this.executionTime = executionTime;
	}
	/**
	 * @return the consultantName
	 */
	public String getConsultantName() {
		return consultantName;
	}
	/**
	 * @param consultantName the consultantName to set
	 */
	public void setConsultantName(String consultantName) {
		this.consultantName = consultantName;
	}
	/**
	 * @return the supervisor
	 */
	public String getSupervisor() {
		return supervisor;
	}
	/**
	 * @param supervisor the supervisor to set
	 */
	public void setSupervisor(String supervisor) {
		this.supervisor = supervisor;
	}
	
	/**
	 * @return the student
	 */
	public String getStudent() {
		return student;
	}

	/**
	 * @param student the student to set
	 */
	public void setStudent(String student) {
		this.student = student;
	}
	
	public boolean validate() {
		return true;
	}

	public void startProcess() {
		ProcessEngine engine = ThesisUtils.PROCESS_ENGINE;
		RepositoryService repositoryService = engine.getRepositoryService();
		ExecutionService executionService = engine.getExecutionService();

		long processDefCount = repositoryService.createProcessDefinitionQuery().processDefinitionId(ThesisUtils.PROCESS_DEFINITION_ID).count();
		if (processDefCount == 0) {
			repositoryService.createDeployment().addResourceFromClasspath("thesisSubmission.jpdl.xml").deploy();
		}
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("title", title);
		variables.put("annotation", annotation);
		variables.put("description", description);
		variables.put("programmingTechnologies", programmingTechnologies);
		variables.put("executionTime", executionTime);
		variables.put("consultantName", consultantName);
		variables.put("supervisor", supervisor);
		variables.put("student", student);
		variables.put("status", "waiting");
		variables.put("submitted", new Date());
		ProcessInstance instance = executionService.startProcessInstanceByKey(ThesisUtils.PROCESS_KEY, variables);
	}

}
